(function (w) {
    const electron = window.top.require("electron");
    const BrowserWindow = electron.remote.BrowserWindow;
    const path = window.top.require('path');
    const Store = window.top.require('electron-store');
    const store = new Store();
    const fs = window.top.require('fs');

    let companyInfo = fs.readFileSync(window.top.__dirname + '/company.ini', 'utf8');

    let printWin;

    function printReceipt(config) {
        db.selectBysql("select * from t_company ", [],
            function (d) {
                console.log(d);
                var company_save = d[0];
                var _conf = {
                    printData: config.printData ? JSON.parse(JSON.stringify(config.printData)) : {},
                    printPagePath: config.printPagePath || '/demo/testprint.html',
                    width: config.width || 300,
                    height: config.height || 300,
                    callback: config.callback || null
                };
                var comp = eval("(" + companyInfo + ")");

                //联系电话为空就调用本地数据库电话
                if (isNull(comp.tel)) {
                    comp.tel = company_save.phone;
                }


                $.extend(_conf.printData, {"comp": comp});
                store.set("printData", _conf.printData);
                const modalPath = path.join('file://', window.top.__dirname, _conf.printPagePath);
                printWin = new BrowserWindow({
                    width: _conf.width,
                    height: _conf.height,
                    show: false
                });
                printWin.on('close', function () {
                    printWin = null;
                });
                printWin.loadURL(modalPath);
                printWin.once("ready-to-show", () => {
                    setTimeout(function () {
                        printWin.webContents.print({
                            silent: false,
                            printBackground: true

                        }, function (success) {
                            printWin = null;
                        });
                        if (_conf.callback) {
                            _conf.callback();
                        }
                        printWin = null;
                        store.delete(_conf.storeName);
                    }, 500);

                });
            });


    }

    w.printReceipt = printReceipt;
})(window);
